package com.iap.app.util
{
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.Event;
	
	import mx.core.UIComponent;
	import mx.effects.Parallel;
	import mx.effects.Sequence;
	import mx.effects.Zoom;
	import mx.events.EffectEvent;
	
	import spark.components.Group;
	import spark.effects.Animate;
	import spark.effects.Fade;
	import spark.effects.Move;
	import spark.effects.Rotate3D;
	import spark.effects.Scale;
	import spark.effects.Scale3D;
	import spark.effects.easing.EaseInOutBase;
	import spark.effects.easing.EasingFraction;
	import spark.effects.easing.Power;
	import spark.effects.easing.Sine;

	public class GlideTransitionEffect
	{
		public static function start(target1:UIComponent, target2:UIComponent, transition:uint = 0):void
		{
			switch(transition)
			{
				case 0:
				{
					GlideInStart(target1, target2);
					break;
				}
				case 1:
				{
					GlideOutStart(target1, target2);
					break;
				}
				default:
				{
					GlideInStart(target1, target2);
					break;
				}
			}
		}
		
		public static function GlideInStart(target1:UIComponent, target2:UIComponent):void
		{
			target1.alpha = 0;
			
			var parallel2:Parallel = new Parallel(target2);
			parallel2.duration = 200;
			
			var scale3d2:spark.effects.Scale3D = new Scale3D();
			scale3d2.autoCenterTransform = true;
			scale3d2.scaleXFrom = 1;
			scale3d2.scaleXTo = 1.5;
			scale3d2.scaleYFrom = 1;
			scale3d2.scaleYTo = 1.5;
			scale3d2.scaleZFrom = 1;
			scale3d2.scaleZTo = 1.5;
			scale3d2.easer = new spark.effects.easing.Power(0.5);
			
			var fader2:Fade = new Fade();
			fader2.alphaFrom = 1;
			fader2.alphaTo = 0;
			fader2.easer = new spark.effects.easing.Power(0.5);
			
			parallel2.addChild(fader2);
			parallel2.addChild(scale3d2);
			
			var parallel:Parallel = new Parallel(target1);
			parallel.duration = 300;
			
			var fader1:Fade = new Fade();
			fader1.alphaFrom = 0.5;
			fader1.alphaTo = 1;
			
			var scale3d1:spark.effects.Scale3D = new Scale3D();
			scale3d1.autoCenterTransform = true;
			scale3d1.scaleXFrom = 0.9;
			scale3d1.scaleXTo = 1;
			scale3d1.scaleYFrom = 0.9;
			scale3d1.scaleYTo = 1;
			scale3d1.scaleZFrom = 0.9;
			scale3d1.scaleZTo = 1;
			scale3d1.easer = new spark.effects.easing.Sine(0);
			
			parallel.addChild(fader1);
			parallel.addChild(scale3d1);
			
			var seq:Sequence = new Sequence();
			seq.duration = 500;
			seq.addChild(parallel2);
			seq.addChild(parallel);
			
			target1.setStyle("creationCompleteEffect", seq);
			
			var fxFnc:Function;
			target2.addEventListener(EffectEvent.EFFECT_END, fxFnc = function(evt:Event):void{
				target2.removeEventListener(EffectEvent.EFFECT_END, fxFnc);
				if(target2==null) return;
				if(target2.parent==null) return;
				(target2.parent as Group).removeElement(target2);
			});
		}

		public static function GlideOutStart(target1:UIComponent, target2:UIComponent):void
		{
			target1.alpha = 0;
			
			var parallel2:Parallel = new Parallel(target2);
			parallel2.duration = 400;
				
			var scale3d2:spark.effects.Scale3D = new Scale3D();
			scale3d2.autoCenterTransform = true;
			scale3d2.scaleXFrom = 1;
			scale3d2.scaleXTo = .7;
			scale3d2.scaleYFrom = 1;
			scale3d2.scaleYTo = .7;
			scale3d2.scaleZFrom = 1;
			scale3d2.scaleZTo = .7;
			scale3d2.easer = new spark.effects.easing.Power(0.3);
			
			var fader2:Fade = new Fade();
			fader2.alphaFrom = 1;
			fader2.alphaTo = 0;
			fader2.easer = new spark.effects.easing.Power(0.3);

			parallel2.addChild(scale3d2);
			parallel2.addChild(fader2);

			var parallel:Parallel = new Parallel(target1);
			parallel.duration = 400;

			var fader1:Fade = new Fade();
			fader1.alphaFrom = 0;
			fader1.alphaTo = 1;
			fader1.easer = new spark.effects.easing.Power(0.3);
			
			var scale3d1:spark.effects.Scale3D = new Scale3D();
			scale3d1.autoCenterTransform = true;
			scale3d1.scaleXFrom = 1.3;
			scale3d1.scaleXTo = 1;
			scale3d1.scaleYFrom = 1.3;
			scale3d1.scaleYTo = 1;
			scale3d1.scaleZFrom = 1.3;
			scale3d1.scaleZTo = 1;
			scale3d1.easer = new spark.effects.easing.Power(0.3);
			
			parallel.addChild(scale3d1);
			parallel.addChild(fader1);
			
			var seq:Sequence = new Sequence();
			seq.addChild(parallel2);
			seq.addChild(parallel);

			target1.setStyle("creationCompleteEffect", seq);

			var fxFnc:Function;
			target2.addEventListener(EffectEvent.EFFECT_END, fxFnc = function(evt:Event):void{
				target2.removeEventListener(EffectEvent.EFFECT_END, fxFnc);
				if(target2==null) return;
				if(target2.parent==null) return;
				(target2.parent as Group).removeElement(target2);
			});
		}

		public static function rotate3DFront(view:Object):void
		{
			var rotate:Rotate3D = new Rotate3D();
			rotate.angleXFrom = 0;
			rotate.angleXTo = 360;
			rotate.autoCenterProjection = true;
			rotate.autoCenterTransform = true;
			rotate.duration = 500 ;
			rotate.target = view;
			rotate.play();
		}

		public static function rotate3Dback(view:Object):Animate
		{
			var rotate:Rotate3D = new Rotate3D();
			rotate.angleXFrom = 0;
			rotate.angleXTo = 360;
			rotate.autoCenterProjection = true;
			rotate.autoCenterTransform = true;
			rotate.duration = 500 ;
			rotate.target = view;
			rotate.play();
			return rotate;
		}
	}
}